package cn.piaoyi.service.imp;

import java.util.*;
import org.apache.commons.logging.*;
import cn.piaoyi.bean.Comment;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.dao.CommentDAO;
import cn.piaoyi.dao.GoodsDAO;
import cn.piaoyi.dao.UserInfoDAO;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.CommentService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;


public class CommentServiceImp implements CommentService {

	private static final Log logger = LogFactory.getLog(CommentServiceImp.class);
	
	private CommentDAO commentDAO;
	private GoodsDAO goodsDAO;
	private UserInfoDAO userInfoDAO;
	

	public CommentDAO getCommentDAO() {
		return commentDAO;
	}


	public void setCommentDAO(CommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}
	
	public GoodsDAO getGoodsDAO() {
		return goodsDAO;
	}

	public void setGoodsDAO(GoodsDAO goodsDAO) {
		this.goodsDAO = goodsDAO;
	}
	
	public UserInfoDAO getUserInfoDAO() {
		return userInfoDAO;
	}

	public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}

	/**
	 * 保存Comment对象
	 * 
	 * @param comment
	 * 			Comment对象
	 * @return Comment
	 * 			Comment对象
	 */
	public Comment createComment(Comment comment) throws PiaoyiException{
		try {
			comment = this.getCommentDAO().saveComment(comment);
			return comment;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}

	
	/**
	 * 保存Comment对象
	 * 
	 * @param comment
	 * 			Comment对象
	 * @return Comment
	 * 			Comment对象
	 */
	public Comment updateComment(Comment comment) throws PiaoyiException{
		try {
			comment = this.getCommentDAO().updateComment(comment);
			return comment;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}
	
	/**
	 * 根据主键ID获取Comment对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Comment
	 * 			Comment对象
	 */
	public Comment findCommentById(long id){
		Comment c =  this.getCommentDAO().findCommentById(id);
		c.setGoods(this.getGoodsDAO().findGoodsById(c.getGoodsId()));
		if(c.getUserId() == 0){
			UserInfo ui = new UserInfo();
			ui.setId(0);
			ui.setAccount("游客");
			c.setUserInfo(ui);
		}else{
			c.setUserInfo(this.getUserInfoDAO().findUserInfoById(c.getUserId()));
		}
		return c;
	}
	
	/**
	 * 获取Comment列表
	 * 
	 * @param goodsId
	 * 			long
	 * @param isShow
	 * 			int
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param pages
	 * 			  Pages
	 * @return PageList
	 * 			Comment对象列表
	 */
	public PageList findComments(long goodsId,long userId,int isShow,String orderby, int ascOrDesc,Pages pages){
		PageList pl = new PageList();
        if (pages.getTotalNum() == -1) {
            pages.setTotalNum(this.getCommentDAO().getCommentNum(goodsId, userId, isShow));
        }
        pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
		List l = this.getCommentDAO().findComments(goodsId, userId, isShow,orderby, ascOrDesc,pages.getSpage(), pages.getPerPageNum());
		if(l == null || l.isEmpty()){
	   		 pl.setObjectList(null);
	   	     pl.setPages(pages);
	   		 return pl;
		}else{
			for(int i=0;i<l.size();i++){
				Comment c = (Comment) l.get(i);
				c.setGoods(this.getGoodsDAO().findGoodsById(c.getGoodsId()));
				if(c.getUserId() == 0){
					UserInfo ui = new UserInfo();
					ui.setId(0);
					ui.setAccount("游客");
					c.setUserInfo(ui);
				}else{
					c.setUserInfo(this.getUserInfoDAO().findUserInfoById(c.getUserId()));
				}
			}
			pl.setObjectList(l);
	        pl.setPages(pages);
	        return pl;
		}
	}
	
	/**
	 * 获取Comment列表
	 * 
	 * @param goodsId
	 * 			long
	 * @param isShow
	 * 			int
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			Goods对象列表
	 */
	public List findComments(long goodsId,long userId,int isShow,String orderby, int ascOrDesc, int firstResult, int maxResults){
		List l = this.getCommentDAO().findComments(goodsId, userId, isShow,orderby, ascOrDesc,firstResult, maxResults);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			for(int i=0;i<l.size();i++){
				Comment c = (Comment) l.get(i);
				c.setGoods(this.getGoodsDAO().findGoodsById(c.getGoodsId()));
				if(c.getUserId() == 0){
					UserInfo ui = new UserInfo();
					ui.setId(0);
					ui.setAccount("游客");
					c.setUserInfo(ui);
				}else{
					c.setUserInfo(this.getUserInfoDAO().findUserInfoById(c.getUserId()));
				}
			}
			return l;
		}
	}
	
	/**
	 * 获取Comment列表
	 * 
	 * @param goodsId
	 * 			long
	 * @param isShow
	 * 			int
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @return List
	 * 			Comment对象列表
	 */
	public List findComments(long goodsId,long userId,int isShow,String orderby, int ascOrDesc){
		List l = this.getCommentDAO().findComments(goodsId, userId, isShow,orderby, ascOrDesc);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			for(int i=0;i<l.size();i++){
				Comment c = (Comment) l.get(i);
				c.setGoods(this.getGoodsDAO().findGoodsById(c.getGoodsId()));
				if(c.getUserId() == 0){
					UserInfo ui = new UserInfo();
					ui.setId(0);
					ui.setAccount("游客");
					c.setUserInfo(ui);
				}else{
					c.setUserInfo(this.getUserInfoDAO().findUserInfoById(c.getUserId()));
				}
			}
			return l;
		}
	}
	
	/**
	 * 删除Comment对象
	 */
	public void removeCommentById(long id){
		this.getCommentDAO().removeCommentById(id);
	}
}
